import 'jsignature';
import 'bootstrap-validator';
import { post } from 'utils/request';

const $sig = $("#handwritten_num");
const $form = $('#myForm');
const $btn = $form.find('#submit');
const $result = $('#result');

$sig.jSignature({ lineWidth: 8, width: '100%', height: '100%' });
$('#rewrite').on('click', () => $sig.jSignature('reset'));

$('#myForm').validator().on('submit', function (e) {
  if (e.isDefaultPrevented()) {
    console.log('表单验证不通过');
  } else {
    e.preventDefault();

    $('#result').val('稍等...');

    let post_data = {
      model: this.model.value,
      image: $sig.jSignature('getData'),
    };

    $btn.button('loading');
    post('ai/handwritten_num', post_data).done(d => {
      $result.val(d);
    }).fail(d => {
      $result.val(d.toString());
    }).always(() => {
      $btn.button('reset');
    });
  }
});